<template>
  <section>
    <!--工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="query">
        <el-form-item>
          <el-input v-model="query.keyword" placeholder="店铺信息"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="getQueryShops">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>
    <!--列表-->
    <el-table :data="this.pageInfo.dataList" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column type="index" width="60">
      </el-table-column>
      <el-table-column prop="name" label="店铺名字" width="180" sortable>
      </el-table-column>
      <el-table-column prop="tel" label="店铺电话" width="150"  sortable>
      </el-table-column>
      <el-table-column prop="admin.realName" label="管理者" width="100"  sortable>
      </el-table-column>
      <el-table-column prop="address" label="店铺地址" width="250"  sortable>
      </el-table-column>
      <el-table-column prop="state" label="状态" min-width="100" sortable>
        <template slot-scope="scope">
          <span v-if="scope.row.state==1" style="color: goldenrod">待审核</span>
          <span v-if="scope.row.state==2" style="color: #20a0ff">待激活</span>
          <span v-if="scope.row.state==3" style="color: #13ce66">激活成功</span>
          <span v-if="scope.row.state==4" style="color: red">驳回</span>
        </template>
      </el-table-column>
      <el-table-column prop="registerTime" label="注册时间" min-width="120" sortable>
      </el-table-column>
      <el-table-column prop="logo" label="店铺logo" min-width="" sortable>
        <template slot-scope="scope">
          <img :src="scope.row.logo" style="width: 50px;height: 50px">
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
          <el-button type="danger" v-if="scope.row.state==1 ||scope.row.state==4" size="small" @click="handleDel(scope.$index, scope.row)">审核</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!--工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <!--			<el-pagination layout="prev, pager, next" @current-change="handleCurrentChange" :page-size="this.query.pageSize" :total="this.pageInfo.total" style="float:right;">-->
      <!--			</el-pagination>-->
      <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page.sync="this.query.currentPage"
          :page-sizes="[5, 10, 15, 20]"
          :page-size="10"
          layout="total, sizes, prev, pager, next, jumper"
          style="float:right;"
          :total="this.pageInfo.total">
      </el-pagination>
    </el-col>

    <!--新增/修改界面-->
    <!-- v-model弹出模态框有问题 替换为:visible.sync -->
    <el-dialog title="新增/修改" :visible.sync="saveFormVisible" :close-on-click-modal="false">
      <el-form :model="saveForm" label-width="80px"  ref="saveForm">
        <el-form-item label="键" prop="key">
          <el-input v-model="saveForm.key" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="值" prop="value">
          <el-input v-model="saveForm.value" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="介绍" prop="intro">
          <el-input type="textarea" v-model="saveForm.intro"></el-input>
        </el-form-item>
        <el-form-item label="状态">
          <el-radio-group v-model="saveForm.type">
            <el-radio class="radio" :label="1">启用</el-radio>
            <el-radio class="radio" :label="0">禁用</el-radio>
          </el-radio-group>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="saveFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="addSubmit" :loading="addLoading">提交</el-button>
      </div>
    </el-dialog>
  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
export default {
  data() {
    return {
      query: {
        keyword: '',
        currentPage: 1,
        pageSize: 10
      },
      pageInfo:{
        dataList: [],
        total: 0,
      },
      page: 1,
      listLoading: false,
      sels: [],//列表选中列


      saveFormVisible: false,//新增界面是否显示
      addLoading: false,
      //新增界面数据
      saveForm: {
        id: null,
        key: "",
        value: "",
        type: "",
        intro: ""
      },
    }
  },
  methods: {
    handleSizeChange(val){
      this.query.pageSize = val;
      this.getShops()
    },

    // 点击查询按钮
    getQueryShops(){
      this.query.currentPage = 1
      this.getShops()
    },

    handleCurrentChange(val) {
      this.query.currentPage = val;
      this.getShops()
    },

    //获取部门列表
    getShops() {
      this.listLoading = true;
      //发请求
      this.$http.put("/shop",this.query)
          .then(result =>{
            result = result.data
            if (result.success){
              this.$message({message: '查询成功',type: 'success'});
              this.listLoading = false;
              this.pageInfo = result.resultObj
              console.log(result);
            }else {
              this.$message({message: '查询失败',type: 'danger'});
            }
          })
          .catch(result =>{

          })
    },

    //删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true; // 模态框
        //NProgress.start();
        let para = { id: row.id };
        //发请求
        this.$http.delete("/shop/"+row.id)
            .then(result=>{
              result = result.data
              if (result.success){
                this.$message({message: '删除成功', type: 'success' });
                this.listLoading = false;
                this.query.currentPage = 1;
                // 刷新列表
                this.getShops()
              }else{
                this.$message({message: '删除失败', type: 'danger' });
              }
            })
      }).catch(() => {

      });
    },

    //显示编辑界面
    handleEdit: function (index, row) {
      this.saveFormVisible = true;
      this.getShops()
      this.saveForm = Object.assign({}, row);

    },

    //显示新增界面
    handleAdd: function () {
      this.saveFormVisible = true;
      this.saveForm = {
        id: null,
        key: "",
        value: "",
        type: "",
        intro: ""
      }
      this.getEmployees()
      this.getDeptTree()
    },

    //新增
    addSubmit: function () {
      this.$refs.saveForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.addLoading = true;
            let para = Object.assign({}, this.saveForm);
            // 发请求
            this.$http.post("/shop",para)
                .then(result=>{
                  result = result.data
                  if (result.success){
                    this.$message({message: '保存成功', type: 'success' });
                    this.addLoading = false;
                    //关闭新增模态框
                    this.saveFormVisible = false
                    // 刷新列表
                    this.getShops()
                  }else{
                    this.$message({message: '保存失败', type: 'danger' });
                  }
                })
          })
              .catch(() => {
                this.$message({message: '网路走丢啦~', type: 'danger' });
              })
              .catch(()=>{
              })
        }
      });
    },

    selsChange: function (sels) {
      this.sels = sels;
    },

    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除选中记录吗？', '提示', {
        type: 'warning'
      }).then(() => {
        this.listLoading = true;
        //发请求
        this.$http.patch("/shop",ids)
            .then(result=>{
              result = result.data
              if (result.success){
                this.$message({message: '批量删除成功', type: 'success' });
                this.listLoading = false;
                this.query.currentPage = 1;
                // 刷新列表
                this.getShops()
              }else{
                this.$message({message: '批量删除失败', type: 'danger' });
              }
            })
      }).catch(() => {
      });
    }
  },
  mounted() {
    this.getShops();
  }
}

</script>

<style scoped>

</style>